
disp(' Importing micro climate data ...')

Seasons      = {'growth' 'harvest'};
SeasonMonths = {[11 12 1 2 3 4], [5 6 7 8 9 10]};

% NO INSOLATION DATA YET FOR THE MUNIC BASED DATA
% disp(' Creating seasonal insolation ...')
% [GridIns,NamesIns] = dbfread([BaseName,'Ins']);
% GridIns = cell2table(GridIns,'VariableNames',NamesIns); 
% GridIns = table2dataset(GridIns);
% GridIns.Properties.VarNames{2} = 'FID_CSGrid';% Just replace the FID identifier by the neutral FID_Grid
% GridIns.Join_Count = []; % Delete unused variable Join_Count.
% 
% for q=1:2
%     climqName = ['ins_'  Seasons{q}];
%     climvarlist = {};
%     for j=SeasonMonths{q}
%         climvar = ['ins' num2str(j)];
%         climvarlist = [climvarlist climvar];
%     end
%     % Add name:
%     GridIns.Season = mean(double(GridIns(:,climvarlist)),2);
%     GridIns.Properties.VarNames{end} = climqName;
% end

BaseClim = 'G1kmG5kmWGS';

disp(' Creating seasonal mean temp ...')
[GridTmean,NamesTmean] = dbfread([BaseClim,'tmean']);
GridTmean = cell2table(GridTmean,'VariableNames',NamesTmean); 
GridTmean = table2dataset(GridTmean);
GridTmean.Properties.VarNames{2} = 'FID_Grid';% Just replace the FID identifier by the neutral FID_Grid

for q=1:2
    climqName = ['tmean_'  Seasons{q}];
    climvarlist = {};
    for j=SeasonMonths{q}
        climvar = ['tmean' num2str(j)];
        climvarlist = [climvarlist climvar];
    end
    % Add name:
    GridTmean.Season = mean(double(GridTmean(:,climvarlist)),2);
    GridTmean.Properties.VarNames{end} = climqName;
end

disp(' Creating seasonal max temp ...')
[GridTmax,NamesTmax] = dbfread([BaseClim,'tmax']);
GridTmax = cell2table(GridTmax,'VariableNames',NamesTmax); 
GridTmax = table2dataset(GridTmax);
GridTmax.Properties.VarNames{2} = 'FID_Grid';% Just replace the FID identifier by the neutral FID_Grid

for q=1:2
    climqName = ['tmax_'  Seasons{q}];
    climvarlist = {};
    for j=SeasonMonths{q}
        climvar = ['tmax' num2str(j)];
        climvarlist = [climvarlist climvar];
    end
    % Add name:
    GridTmax.Season = mean(double(GridTmax(:,climvarlist)),2);
    GridTmax.Properties.VarNames{end} = climqName;
end

disp(' Creating seasonal min temp ...')
[GridTmin,NamesTmin] = dbfread([BaseClim,'tmin']);
GridTmin = cell2table(GridTmin,'VariableNames',NamesTmin); 
GridTmin = table2dataset(GridTmin);
GridTmin.Properties.VarNames{2} = 'FID_Grid';% Just replace the FID identifier by the neutral FID_Grid

for q=1:2
    climqName = ['tmin_'  Seasons{q}];
    climvarlist = {};
    for j=SeasonMonths{q}
        climvar = ['tmin' num2str(j)];
        climvarlist = [climvarlist climvar];
    end
    % Add name:
    GridTmin.Season = mean(double(GridTmin(:,climvarlist)),2);
    GridTmin.Properties.VarNames{end} = climqName;
end

disp(' Creating seasonal precipitation ...')
[GridPrec,NamesPrec] = dbfread([BaseClim,'prec']);
GridPrec = cell2table(GridPrec,'VariableNames',NamesPrec); 
GridPrec = table2dataset(GridPrec);
GridPrec.Properties.VarNames{2} = 'FID_Grid';% Just replace the FID identifier by the neutral FID_Grid

for q=1:2
    climqName = ['prec_'  Seasons{q}];
    climvarlist = {};
    for j=SeasonMonths{q}
        climvar = ['prec' num2str(j)];
        climvarlist = [climvarlist climvar];
    end
    % Add name:
    GridPrec.Season = mean(double(GridPrec(:,climvarlist)),2);
    GridPrec.Properties.VarNames{end} = climqName;
end

% Merge separate climate datasets:
GridClimate = GridTmean(:,{'FID_Grid' 'tmean_harvest' 'tmean_growth'}); % Start with GridIns as base.
GridClimate = join(GridClimate,GridTmin(:,{'FID_Grid' 'tmin_harvest' 'tmin_growth' }),'Keys','FID_Grid');
GridClimate = join(GridClimate,GridTmax(:,{'FID_Grid' 'tmax_harvest' 'tmax_growth' }),'Keys','FID_Grid');
GridClimate = join(GridClimate,GridPrec(:,{'FID_Grid' 'prec_harvest' 'prec_growth' }),'Keys','FID_Grid');
clear GridTmin GridTmax GridPrec GridTmean
